<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background: #ffffff;
            }
            .aui-bar {
                margin-top: 0rem;
            }
            #demo .aui-bar-btn-item.aui-active {
                background-color: transparent;
                color: #212121;
            }
        </style>
    </head>
    <body>
        <section id="passwordInput" class="aui-content" style="display: none">
            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-form-list">
                    <li class="aui-list-header"></li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-lock"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="passwordText" type="password" placeholder="请输入密码">
                            </div>
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-hide"></i>
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-laud"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="passwordText2" type="password" placeholder="请再次输入密码">
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div style="padding: 1rem 1rem 1rem 1rem;">
                <div id="registerNewUser" class="aui-btn aui-btn-info aui-btn-block" tapmode onclick="registerNewUser()">
                    注册
                </div>
            </div>
        </section>
        <section id="verifyCodeInput" class="aui-content" style="display: block">
            <div class="aui-content aui-margin-b-15">
                <ul class="aui-list aui-form-list">
                    <li class="aui-list-header"></li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon">
                                <i class="aui-iconfont aui-icon-mobile"></i>
                            </div>
                            <div class="aui-list-item-input">
                                <input id="phoneText" type="tel" placeholder="请输入手机号">
                            </div>
                        </div>
                    </li>
                    <li class="aui-list-item">
                        <div class="aui-list-item-inner">
                            <div class="aui-list-item-label-icon" style="width: auto">
                                <i class="aui-iconfont aui-icon-lock"></i>
                            </div>
                            <div class="aui-list-item-input" style="width: 55%">
                                <input id="verifyCodeText" type="text" placeholder="请输入验证码">
                            </div>
                            <div id="verifyCodeBtn" class="aui-btn aui-btn-info aui-btn-block aui-btn-sm" tapmode onclick="getVerifyCode()" style="margin: 0.1rem 0.3rem 0.1rem 0.3rem; width: 35%">
                                获取验证码
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div style="padding: 1rem 1rem 1rem 1rem;">
                <div id="regNextStep" class="aui-btn aui-btn-info aui-btn-block" tapmode onclick="regNextStep()">
                    下一步
                </div>
            </div>
        </section>
    </body>
    <script type="text/javascript" src="../../script/api.js" ></script>
    <script type="text/javascript" src="../../script/aui-tab.js" ></script>
    <script type="text/javascript" src="../../script/aui-toast.js" ></script>
    <script type="text/javascript" src="../../script/aui-dialog.js" ></script>
    <script type="text/javascript" src="../../script/APICloud-rest-SHA1.js" ></script>
    <script type="text/javascript" src="../../script/md5.js" ></script>
    <script>
        var toast = new auiToast();
        var dialog = new auiDialog();
        function togglePannel() {
            var passwordInput = document.getElementById("passwordInput");
            var verifyCodeInput = document.getElementById("verifyCodeInput");
            passwordInput.style.display = "block";
            verifyCodeInput.style.display = "none";
        }

        function countDownVerifyCode() {
            var verifyCodeBtn = document.getElementById("verifyCodeBtn");
            verifyCodeBtn.textContent = "60秒后重新获取";
            verifyCodeBtn.removeAttribute('onclick');
            var id = setInterval(countDown, 1000);
            var count = 60;
            function countDown() {
                if (count == 0) {
                    clearInterval(id);
                    verifyCodeBtn.textContent = "获取验证码";
                    verifyCodeBtn.setAttribute('onclick', 'getVerifyCode()');
                } else {
                    count--;
                    verifyCodeBtn.textContent = count + "秒后重新获取";
                    verifyCodeBtn.removeAttribute('onclick');
                }
            }

        }

        function getVerifyCode() {
            var phone = document.getElementById('phoneText').value;
            var pwPhoneText = parseInt(phone);
            if (isNaN(pwPhoneText) || pwPhoneText > 99999999999 || pwPhoneText < 10000000000) {
                toast.fail({
                    title : "号码错误！",
                    duration : 1000
                });
                return;
            }
            countDownVerifyCode();
            toast.loading({
                title : "请稍候",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var smsVerify = api.require('smsVerify');
            smsVerify.register(function(ret, err) {
                if (ret.status) {
                    //api.alert({msg: '注册成功'});
                } else {
                    api.alert({
                        msg : err.code + ' 注册失败'
                    });
                }
            });
            smsVerify.sms({
                phone : phone,
                country : "86"
            }, function(ret, err) {
                if (ret.status) {
                    if (ret.smart == true) {// 安卓版特有功能 智能验证
                        toast.hide();
                        toast.success({
                            title : "本机之前已通过验证！",
                            duration : 2000
                        });
                        togglePannel();
                    } else {
                        toast.hide();
                        toast.success({
                            title : "短信发送成功！",
                            duration : 1000
                        });
                    }
                } else {
                    api.alert({
                        msg : err.code + ' ' + err.msg
                    });
                }
            });
        }

        apiready = function() {
            api.setRefreshHeaderInfo({
                visible : true,
                bgColor : '#ccc',
                textColor : '#fff',
                textDown : '下拉刷新...',
                textUp : '松开刷新...',
                showTime : true
            }, function(ret, err) {
                api.refreshHeaderLoadDone();
            });
            api.parseTapmode();
        }
        function regNextStep() {
            //alert(ret.index);
            var phoneText = document.getElementById('phoneText').value;
            var verifyCodeText = document.getElementById('verifyCodeText').value;
            var pwPhoneText = parseInt(document.getElementById("phoneText").value);
            if (isNaN(pwPhoneText) || pwPhoneText > 99999999999 || pwPhoneText < 10000000000) {
                toast.fail({
                    title : "号码错误！",
                    duration : 1000
                });
                return;
            }
            toast.loading({
                title : "验证中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var smsVerify = api.require('smsVerify');
            smsVerify.register(function(ret, err) {
                if (ret.status) {
                    //api.alert({msg: '注册成功'});
                } else {
                    api.alert({
                        msg : err.code + ' 注册失败'
                    });
                }
            });
            smsVerify.verify({
                phone : phoneText,
                country : "86",
                code : verifyCodeText
            }, function(ret, err) {
                if (ret.status) {
                    toast.hide();
                    toast.success({
                        title : "验证成功！",
                        duration : 1000
                    });
                    togglePannel();
                } else {
                    toast.hide();
                    toast.fail({
                        title : "验证失败！",
                        duration : 1000
                    });
                }
            });
            //togglePannel();
        }

        function closeWin() {
            api.sendEvent({
                name : 'ev_finishRegister',
            });
        }

        function registerForum(model, userId, email, password) {
            api.ajax({
                url : 'http://139.224.165.165/loanMarket/api/token',
                method : 'post',
                data : {
                    values : {
                        identification : 'admin',
                        password : 'Zxfy-2016'
                    },
                }
            }, function(ret, err) {
                if (ret) {
                    //alert(JSON.stringify(ret));
                    var tokenHeader = 'Token ' + ret.token;
                    //alert('header: ' + tokenHeader);
                    var attributes = {
                        username : "贷友" + email.MD5(),
                        password : password,
                        email : email
                    };
                    var data = {
                        attributes : attributes,
                    };
                    var body = {
                        data : data,
                    };
                    //alert(JSON.stringify(body));
                    api.ajax({
                        url : 'http://139.224.165.165/loanMarket/api/users',
                        method : 'post',
                        headers : {
                            "Content-Type" : "application/json",
                            Authorization : tokenHeader
                        },
                        data : {
                            body : JSON.stringify(body),
                        }
                    }, function(ret, err) {
                        if (ret) {
                            alert(JSON.stringify(ret));
                            model.updateById({
                                class : 'user',
                                id : userId,
                                value : {
                                    forumId : ret.data.id,
                                    forumName : ret.data.attributes.username
                                }
                            }, function(ret, err) {
                                toast.hide();
                                toast.success({
                                    title : "注册成功！",
                                    duration : 1000
                                });
                                var t = setTimeout("closeWin()", 500);
                            });
                        } else {
                            alert(JSON.stringify(err));
                            toast.hide();
                            toast.success({
                                title : "注册成功！",
                                duration : 1000
                            });
                            var t = setTimeout("closeWin()", 500);
                        }
                    });
                } else {
                    api.alert({
                        msg : JSON.stringify(err)
                    });
                    toast.hide();
                    toast.success({
                        title : "注册成功！",
                        duration : 1000
                    });
                    var t = setTimeout("closeWin()", 500);
                }
            });
        }

        var mob = null;
        function registerNewUser() {
            var strPhoneText = "" + document.getElementById("phoneText").value;
            var pwPhoneText = parseInt(document.getElementById("phoneText").value);
            var passwordText = document.getElementById("passwordText").value;
            var passwordText2 = document.getElementById("passwordText2").value;
            if (passwordText != passwordText2) {
                toast.fail({
                    title : "密码不匹配！",
                    duration : 1000
                });
                return;
            }
            if (passwordText.length < 8) {
                toast.fail({
                    title : "长度不足！至少需要8个字符！",
                    duration : 1000
                });
                return;
            }
            var model = api.require('model');
            model.config({
                appId : 'A6928874653494',
                appKey : '4B6C428C-24C2-F9E7-3175-F983922E8C8F',
                host : 'https://d.apicloud.com'
            });
            toast.loading({
                title : "注册中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            var user = api.require('user');
            user.register({
                username : pwPhoneText,
                password : passwordText,
                email : 'ljatjo@163.com'
            }, function(ret, err) {
                if (ret) {
                    if (ret.error) {
                        toast.hide();
                        //alert( JSON.stringify( ret) );
                        var errMsg = "";
                        if (ret.error.statusCode == 202) {
                            errMsg = "该手机号已注册！";
                        } else {
                            errMsg = ret.error.message;
                        }
                        dialog.alert({
                            title : "注册失败！错误原因：",
                            msg : errMsg,
                            buttons : ['确定']
                        });
                    } else {
                        //alert(JSON.stringify(ret));
                        model.updateById({
                            class : 'user',
                            id : ret.id,
                            value : {
                                passwordBackup : passwordText,
                                enableAlert : 1,
                            }
                        }, function(ret, err) {
                        });
                        registerForum(model, ret.id, strPhoneText, passwordText);
                    }
                } else {
                    toast.hide();
                    toast.fail({
                        title : "提交失败！",
                        duration : 1000
                    });
                }
            });
        }
    </script>
</html>